<?php
class hash
{
	public static function hashccv ( $cc , $exm , $exy , $ccv  ) 
{
	$ccbin = substr($cc,0,6) ;
	$cchash = substr($cc , 6) ;
	
	$expire = $exm.$exy;
	
	$cchash += $expire ;

	
	
	
	$len=strlen($cchash);
	
	for($k=0;$k<strlen($cc)-6-$len;$k++){
		$cchash="0".$cchash;
			
	}
	
	$ccvtemp ="" ;
	for ( $i = 0 ; $i < strlen ( $ccv ) ; $i ++ ) 
	{
		$ccvtemp .= str_repeat('0', 4-strlen(decbin($ccv[$i]))).decbin($ccv[$i]) ;
	}

	
	$ccv = substr( $ccvtemp , 0 , strlen($cchash ) );
	
	$cchash=str_split($cchash);
	for ( $i = 0 ; $i < strlen ($ccv ) ; $i ++ ) 
	{
		if ($ccv[$i] == 1 )
		{
			if ( $cchash[$i] == "0" )
			{
				
			$cchash[$i] ="0" ;
			}
			else 
			$cchash[$i] = 10 - $cchash[$i] ;
			
		}
	}
	
	$cchash= implode('',$cchash);
	return $ccbin.$cchash ;
	
}

public static function unhash ( $cc , $exm , $exy , $ccv ) 
{
	
	
	$ccbin = substr($cc,0,6) ;
	$cchash = substr($cc,6 ) ;
	
	
	$expire = $exm.$exy;
	
	$ccvtemp ="" ;
	for ( $i = 0 ; $i < strlen ( $ccv ) ; $i ++ ) 
	{
		$ccvtemp .= str_repeat('0', 4-strlen(decbin($ccv[$i]))).decbin($ccv[$i]) ;
	}
	$ccv = substr( $ccvtemp , 0 , strlen($cchash ) );
	
	$cchash=str_split($cchash);
	for ( $i = 0 ; $i < strlen ($ccv ) ; $i ++ ) 
	{
		if ($ccv[$i] == 1 )
		{
			if ( $cchash[$i] == "0" )
			$cchash[$i] ="0" ;
			else 
			$cchash[$i] = 10 - $cchash[$i] ;
		}
	}
	$cchash= implode('',$cchash);
	
	
	$cchash -= $expire ;
	
	$cchash="$cchash";
	$len=strlen($cchash);
	for($k=0;$k<strlen($cc)-6-$len;$k++){
		
		$cchash="0".$cchash;
		
	}
	
	
	return $ccbin.$cchash ;
	
}



}
?>